using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;
using System.Text;

namespace GeneralMinioApi.Controllers
{
    [ApiController]
    [Route("api/mock")]
    public class MockController : ControllerBase
    {
        private readonly IConfiguration _config;

        public MockController(IConfiguration config)
        {
            _config = config;
        }

        /// <summary>
        /// 生成HMAC-SHA256签名
        /// </summary>
        /// <returns>签名字符串</returns>
        [HttpPost("generate-signature")]
        public async Task<IActionResult> GenerateSignature()
        {
            var apiKey = _config?["ApiKey"] ?? "";
            var timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmm");

            // 生成签名内容
            var signature = ComputeHmacSHA256(timestamp, apiKey);
            return Ok(new { timestamp, signature });
        }

        private static string ComputeHmacSHA256(string data, string key)
        {
            using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
            var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(data));
            return BitConverter.ToString(hash).Replace("-", "").ToLower();
        }
    }
}